﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using System.Collections;
using FreedomERP.Comm;
using FreedomERP.Services.Inv;
using System.Web.Routing;
using FreedomERP.Services.Inv.Impl;
using FreedomERP.Models;
using FreedomERP.Models.Inv;
using Newtonsoft.Json;
using FreedomERP.Comm.Utils;
using FreedomERP.Models.Bd;
using FreedomERP.Services.Bd.Impl;
using System.IO;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class ReceiveOrderController : Controller
    {
        IInvReceiveOrderService receiveOrderService { get; set; }
        IBdCategoryService categoryService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (receiveOrderService == null)
            {
                receiveOrderService = new InvReceiveOrderServiceImpl();
            }
            if (categoryService == null)
            {
                categoryService = new BdCategoryServiceImpl();
            }

            base.Initialize(requestContext);
        }

        /// <summary>
        /// 收货单列表页面
        /// 作者：梁建东
        /// </summary>
        public ActionResult ReceiveOrderIndex()
        {
            return View();
        }

        /// <summary>
        /// 收货单列表分页
        /// 作者：梁建东
        /// </summary>
        public AjaxStoreResult ReceiveOrderListPaging(int start, int limit, string dir, string sort, string FNumberParam, string FBillDateFrom, string FBillDateTo, string FStatusId)
        {
            int tatalCount = 0;

            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

            Hashtable filters = new Hashtable();
            filters.Add("FNumberParam", FNumberParam);
            filters.Add("FBillDateFrom", FBillDateFrom);
            filters.Add("FBillDateTo", FBillDateTo);
            filters.Add("FStatusId", FStatusId);
            filters.Add("orgunitParam", currAccountOrgunitFId);

            PagingParams pgp = new PagingParams(sort, OrderDirection.ToDirection(dir), start, limit, filters);

            List<InvReceiveOrder> listTemp = receiveOrderService.QueryReceiveOrderPaggingByParam(pgp, out tatalCount);

            List<InvReceiveOrderView> listNew = new List<InvReceiveOrderView>();
            foreach (InvReceiveOrder objTemp in listTemp)
            {
                InvReceiveOrderView objView = new InvReceiveOrderView();
                objView.FId = objTemp.FId;
                objView.FNumber = objTemp.FNumber;
                objView.FBillDate = DateUtils.FormatDate(objTemp.FBillDate, "yyyy-MM-dd");
                objView.FCreateTime = objTemp.FCreateTime.ToString();
                objView.FCreatorId = objTemp.BdAccount1 == null ? "" : objTemp.BdAccount1.FAccount;
                objView.FStatus = objTemp.BdCategory == null ? "" : objTemp.BdCategory.FName;
                listNew.Add(objView);
            }
            AjaxStoreResult pagingResult = new AjaxStoreResult(listNew, tatalCount);
            return pagingResult;
        }

        /// <summary>
        /// 保存收货单信息
        /// 作者：梁建东
        /// </summary>
        public AjaxResult SaveReceiveOrderMsg(string msg)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string FId = "";
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

                InvReceiveOrderView receiveOrderView = JsonConvert.DeserializeObject<InvReceiveOrderView>(msg);
                if (receiveOrderView.FId == null || receiveOrderView.FId.Trim().Equals(""))
                {
                    receiveOrderView.FOrgunitId = currAccountOrgunitFId;
                    receiveOrderService.AddReceiveOrder(receiveOrderView, currAccountFId, out FId);
                }
                else
                {
                    receiveOrderService.ModifyReceiveOrder(receiveOrderView, currAccountFId);
                    FId = receiveOrderView.FId;
                }
                response.Result = FId;
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取收货单信息
        /// 作者：梁建东
        /// </summary>
        public AjaxStoreResult GetReceiveOrderMsgByFId(string FId)
        {
            List<InvReceiveOrderView> list = new List<InvReceiveOrderView>();
            InvReceiveOrder obj = receiveOrderService.GetReceiveOrderByFId(FId);
            if (obj != null)
            {
                InvReceiveOrderView objView = new InvReceiveOrderView();
                objView.FId = obj.FId;
                objView.FNumber = obj.FNumber;
                objView.FBillDate = DateUtils.FormatDate(obj.FBillDate, "yyyy-MM-dd");
                objView.FCreateTime = obj.FCreateTime.ToString();
                objView.FCreatorId = obj.BdAccount1 == null ? "" : obj.BdAccount1.FAccount;
                objView.FModifyTime = obj.FModifyTime.ToString();
                objView.FModifierId = obj.BdAccount2 == null ? "" : obj.BdAccount2.FAccount;
                objView.FApproveTime = obj.FApproveTime == null ? "" : obj.FApproveTime.ToString();
                objView.FApproverId = obj.BdAccount == null ? "" : obj.BdAccount.FAccount;
                objView.FStatus = obj.FStatusId;
                objView.FRemark = obj.FRemark;

                list.Add(objView);
            }
            return new AjaxStoreResult(list, list.Count);
        }

        /// <summary>
        /// 根据收货单FId获取明细列表
        /// 作者：梁建东
        /// </summary>
        public AjaxStoreResult GetReceiveOrderDetailListByFParentId(string FParentId)
        {
            List<InvReceiveOrderDetailView> listView = new List<InvReceiveOrderDetailView>();
            List<InvReceiveOrderDetail> list = receiveOrderService.GetReceiveOrderDetailListByFParentId(FParentId);
            if (list != null)
            {
                foreach (InvReceiveOrderDetail detail in list)
                {
                    InvReceiveOrderDetailView objView = new InvReceiveOrderDetailView();
                    objView.FId = detail.FId;
                    objView.FProductId = detail.BdProduct.FId;
                    objView.FProductNumber = detail.BdProduct.FNumber;
                    objView.FProductName = detail.BdProduct.FName;
                    objView.FNumber = detail.FNumber;
                    objView.FPrice = detail.FPrice;
                    objView.FAmount = detail.FAmount;
                    objView.FRemark = detail.FRemark;

                    listView.Add(objView);
                }
            }
            return new AjaxStoreResult(listView, listView.Count);
        }

        /// <summary>
        /// 获取订单状态列表
        /// 作者：梁建东
        /// </summary>
        public AjaxStoreResult GetReceiveOrderStatusList()
        {
            List<BdCategory> categoryList = categoryService.QueryCategoryListByFType(ConstData.CATEGORY_RECEIVE_ORDER_STATUS);

            List<BdCategoryView> listView = new List<BdCategoryView>();

            if (categoryList != null)
            {
                foreach (BdCategory category in categoryList)
                {
                    BdCategoryView categoryView = new BdCategoryView();
                    categoryView.FID = category.FId;
                    categoryView.FName = category.FName;
                    listView.Add(categoryView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count);
        }

        /// <summary>
        /// 删除收货订单
        /// 作者：梁建东
        /// </summary>
        public AjaxResult DeleteReceiveOrder(string FId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                receiveOrderService.DeleteReceiveOrder(FId);
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 审核收货订单
        /// 作者：梁建东
        /// </summary>
        public AjaxResult ApproveReceiveOrder(string FId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                receiveOrderService.ApproveReceiveOrder(FId, currAccountFId);
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 反审核收货订单
        /// 作者：梁建东
        /// </summary>
        public AjaxResult UnApproveReceiveOrder(string FId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                receiveOrderService.UnApproveReceiveOrder(FId, currAccountFId);
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 打印条码
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        /*public ActionResult PrintBarcode(string FId)
        {
            string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
            receiveOrderService.PrintBarcode(FId, serverPath, Response);

            return null;
        }*/
    }
}
